# -*- coding:utf-8 -*-

from selenium import webdriver
import urllib2
import time
from xlwt import Workbook

chromedriver_path = "../chromedriver.exe"


def login(self,username,password):
    driver = self.driver
    driver.find_element_by_id("freename").send_keys(username)
    driver.find_element_by_id("freepassword").send_keys(password)
    driver.find_element_by_link_text(u"登录").click()


def exit(self):
    driver = self.driver
    driver.find_element_by_link_text(u"[退出]").click()


def chrome(headless=False):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-gpu')
    if headless is True:
        chrome_options.add_argument('--headless')
    driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chromedriver_path)
    return driver


def web_static(url):

    user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML,' \
                 'like Gecko) Chrome/60.0.3112.90 Safari/537.36'
    headers = {'User-Agent': user_agent}

    try:
        request = urllib2.Request(url, headers=headers)
        response = urllib2.urlopen(request)
        return response.read()
    except Exception as e:

        print u'网页链接错误原因：'
        print e
        return False

